const router=require('koa-router')();

const {
    fetchHotWord,
    fetchHotCourse,
    fetchSuggestion,
} 
= require('../api/search')

router.get('/hotword',async (ctx,next)=>{
    try {
        const data=await fetchHotWord();
        // console.log('---',data,'----')
        let resData={
            code:"0",
            msg:"success"
        }
        if (data.errorCode===0){
            resData.data=data
        }else{
            resData.code="1";
            resData.msg="fail"
        }
        ctx.body=resData
    } catch (error) {
        next(error)
    }
})
router.get('/hotcourse',async (ctx,next)=>{
    try {
        const data=await fetchHotCourse();
        // console.log('---',data,'----')
        let resData={
            code:"0",
            msg:"success"
        }
        if (data.errorCode===0){
            resData.data=data
        }else{
            resData.code="1";
            resData.msg="fail"
        }
        ctx.body=resData
    } catch (error) {
        next(error)
    }
})

router.get('/suggestion',async (ctx,next)=>{
    // console.log(ctx.query.keyword)
    const w = encodeURI(ctx.query.keyword)
    try {
        const data=await fetchSuggestion(w);
        console.log('---',data,'----')
        let resData={
            code:"0",
            msg:"success"
        }
        if (data.errorCode===0){
            resData.data=data
        }else{
            resData.code="1";
            resData.msg="fail"
        }
        ctx.body=resData
    } catch (error) {
        next(error)
    }
})


module.exports=router.routes();